package top.hzy520.user.service;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import top.hzy520.dept.dao.DeptMapper;
import top.hzy520.dept.entity.Dept;
import top.hzy520.user.dao.UserMapper;
import top.hzy520.user.dto.SearchUserDto;
import top.hzy520.user.entity.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 16:30 2018/7/31
 */
@Service
public class UserServiceImpl implements UserService {
    private static final String SYSTEM_DEFAULT_PASSWORD = "123456";

    @Autowired
   private UserMapper userMapper;

   @Autowired
   private DeptMapper deptMapper;
   @Autowired
   private BCryptPasswordEncoder passwordEncoder;

    @Override
    public List<User> selectAllUser(int offset, int limit) {
        return userMapper.selectAllUser(offset,limit);
    }

    @Override
    public int selectAllcount() {
        return userMapper.selectAllcount();
    }

    @Override
    public List<User> selectAllUserById(int offset, int limit, Long id) {
        List<Dept> childrenDeptList=new ArrayList<>();
     List<Dept> deptList=deptMapper.selectDeptByParentId(id);
     List<User> userList=null;
        if (deptList.isEmpty()){
        userList=userMapper.selectAllUserById(offset,limit,id);
     }else {
             selectChildrenDept(deptList,childrenDeptList);
        userList=userMapper.selectUserByDeptList(offset,limit,childrenDeptList);
     }
        return userList;
    }

    private void selectChildrenDept(List<Dept> deptList, List<Dept> childrenDeptList) {
        deptList.stream().forEach(dept->{
                    List<Dept> childrenDept=deptMapper.selectDeptByParentId(dept.getId());
                       if (childrenDept.isEmpty()){
                                 childrenDeptList.add(dept);
                       }else {
                           selectChildrenDept(childrenDept,childrenDeptList);
                       }
                });
    }

    @Override
    public int selectAllcountById(Long id) {
        List<Dept> deptList=deptMapper.selectDeptByParentId(id);
        List<Dept> childrenDeptList=new ArrayList<>();
        if (deptList.isEmpty()){
            return userMapper.selectAllcountBy(id);
        }
      else {
            selectChildrenDept(deptList,childrenDeptList);
            return userMapper.selectCountByListDeptId(childrenDeptList);
        }
    }

    @Override
    public User selectUserByUserId(Long id) {
        User user=userMapper.selectByUserId(id);
      user.setDept(deptMapper.selectByPrimaryKey(user.getDeptId()));
        return user;
    }
    @Override
    public void updataUser(User user) {
     User dbUser=userMapper.selectByPrimaryKey(user.getId());
     if (user!=null){
         dbUser.setEmail(user.getEmail());
         dbUser.setStatus(user.getStatus());
         dbUser.setSex(user.getSex());
         dbUser.setAddress(user.getAddress());
         dbUser.setDeptId(user.getDeptId());
         dbUser.setRealName(user.getRealName());
         dbUser.setRealName(user.getRealName());
         userMapper.updateByPrimaryKeySelective(dbUser);
     }
    }

    @Override
    public void deleteUser(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAllUser(List<User> userList) {
        userMapper.deleteAllUser(userList);
    }

    @Override
    public Long insertUser(User user) {
        user.setUsername(user.getRealName());
        user.setPassword(passwordEncoder.encode(SYSTEM_DEFAULT_PASSWORD));
        userMapper.insertSelective(user);
      return user.getId();
    }
    @Override
    public List<User> selectUserBySearch(SearchUserDto searchUserDto) {
        return userMapper.selectUserBySearch(searchUserDto);
    }

    @Override
    public int selectCountBySearch(SearchUserDto searchUserDto) {
        return userMapper.selectCountBySearch(searchUserDto);
    }

    @Override
    public boolean passwordCheck(String password) {
        User user= (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        return passwordEncoder.matches(password,user.getPassword());
    }

    @Override
    public void resetPwd(User user, String password) {
        user.setPassword(passwordEncoder.encode(password));
          userMapper.updateByPrimaryKeySelective(user);
    }
}
